//
//  SettingHomeViewController.swift
//  EasePods
//
//  Created by 李然 on 2024/4/3.
//

import UIKit
import MoyoungLib
import RxCocoa

enum SettingHomeAction {
case chargingCase, other, firmware, productGuide,about
    
    var title: String {
        
        switch self {
        case .chargingCase:
            return R.string.localizable.setting_earcase_title()
        case .other:
            return R.string.localizable.setting_other_title()
        case .firmware:
            return R.string.localizable.setting_firmware_title()
        case .productGuide:
            return R.string.localizable.setting_product_guide_title()
        case .about:
            return R.string.localizable.setting_about_title()
        }
    }
    
    var icon: UIImage? {
        
        switch self {
        case .chargingCase:
            return R.image.ic_setting_earcase()
        case .other:
            return R.image.ic_setting_other()
        case .firmware:
            return R.image.ic_setting_firmware()
        case .productGuide:
            return R.image.ic_setting_guide()
        case .about:
            return R.image.ic_setting_about()
        }
    }
}

class SettingHomeViewController: ViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = R.color.common_1_bg_main()
    }
    
    override func makeUI() {
        self.view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    override func bindViewModel() {
        
//        let itmes: [SettingHomeAction] = BluetoothManager.shared.isConnected ? [.chargingCase, .other, .firmware,.productGuide, .about] : [.other, .productGuide, .about]
        let itmes: [SettingHomeAction] = BluetoothManager.shared.isConnected ? [.chargingCase, .other, .firmware, .about] : [.other, .about]
        let observable = BehaviorRelay(value: itmes)
        
        observable.bind(to: tableView.rx.items(cellIdentifier: SettingHomeTableViewCell.reuseIdentifier, cellType: SettingHomeTableViewCell.self)) { row, item, cell in
            cell.bind(to: item)
        }.disposed(by: rx.disposeBag)
        
        tableView.rx.itemSelected.subscribe(onNext: { indexPath in
            let item = itmes[indexPath.row]
            switch item {
            case .chargingCase:
                self.dismiss(animated: true)
                let vc = ChargingCaseViewController()
                vc.title = item.title
                RootViewManager.default.currentDisplayingViewController.show(vc, sender: true)
            case .other:
                self.dismiss(animated: true)
                let vc = OtherViewController()
                vc.title = item.title
                RootViewManager.default.currentDisplayingViewController.show(vc, sender: true)
            case .firmware:
                self.dismiss(animated: true)
                let vc = FirmwareViewController()
                vc.title = item.title
                RootViewManager.default.currentDisplayingViewController.show(vc, sender: true)
            case .productGuide:
                self.dismiss(animated: true)
                let vc = ProductGuideViewController()
                vc.title = item.title
                RootViewManager.default.currentDisplayingViewController.show(vc, sender: true)
            case .about:
                self.dismiss(animated: true)
                let vc = AboutViewController()
                vc.title = item.title
                RootViewManager.default.currentDisplayingViewController.show(vc, sender: true)
            }
        }).disposed(by: rx.disposeBag)
    }
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.register(SettingHomeTableViewCell.self, forCellReuseIdentifier: SettingHomeTableViewCell.reuseIdentifier)
        tableView.estimatedRowHeight = 60
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.rowHeight = UITableView.automaticDimension
        tableView.separatorStyle = .none
        tableView.isScrollEnabled = false
        tableView.backgroundColor = .clear
        return tableView
    }()

}
